package request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/** 获取请求行中的数据
 * - GET   /day09_Servlet_war_exploded/myRequest?name=zhangsan  HTTP/1.1
 * - 方法：
 *   1. 获取请求方式：GET
 *      - String getMethod()
 *   2. 获取虚拟目录：/day09_Servlet_war_exploded
 *      - String  getContextPath()
 *   3. 获取Servlet路径：/myRequest
 *      - String  getServletPath()
 *   4. 获取get方式请求参数：name=zhangsan
 *      - String  getQueryString()
 *   5. 获取请求url：/day14/demo1
 *      - String  getRequestURI():     /day09_Servlet_war_exploded/myRequest
 *      - StringBuffer  getRequestURL()   :   http://localhost:8080/day09_Servlet_war_exploded/myRequest
 *   6. 获取协议及版本： HTTP/1.1
 *      - String  getProtocol()
 *   7. 获取客户机的IP地址
 *      - String   getRemoteAddr()
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-03-20-16:44 16:44
 */
@WebServlet("/myRequest")
public class MyRequest extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求方式：GET
        String method = request.getMethod();
        System.out.println(method);
        //2. 获取虚拟目录：/day09_Servlet_war_exploded
        String contextPath = request.getContextPath();
        System.out.println(contextPath);
        //3. 获取Servlet路径：/myRequest
        String servletPath = request.getServletPath();
        System.out.println(servletPath);
        //4.获取get方式请求参数：name=zhangsan
        String queryString = request.getQueryString();
        System.out.println(queryString);
        //5.获取请求url：
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);//  /day09_Servlet_war_exploded/myRequest
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURL);//http://localhost:8080/day09_Servlet_war_exploded/myRequest
        //6. 获取协议及版本：HTTP/1.1
        String protocol = request.getProtocol();
        System.out.println(protocol);
        //7. 获取客户机的IP地址
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
    }
}
